import { Link, useParams } from "react-router-dom";
function Pagination({ data, limit, allData }) {
    let { type = "all", page = "1" } = useParams()
    page = Number(page);
    let pageLen = allData.length;
    if (type !== "all") {
        pageLen = data[type].length
    }
    //console.log(pageLen)
    let pageCount = Math.ceil(pageLen / limit) // Math.cell 向上取整
    //console.log(pageCount)
    let pageEles = [];
    for (let i = 1; i <= pageCount; i++) {
        //console.log(i)
        if (page === i) {
            pageEles.push(<span key={i} className="active"> {i} </span>)
        } else {
            pageEles.push(<Link key={i} to={`/list/${type}/${i}`}> {i} </Link>)
        }
    }
    //console.log(pageEles)

    return (
        <nav className="pagination">
            <Link to={`/list/${type}/${page > 1 ? page - 1 : page}`} onClick={() => {
                if (page <= 1) {
                    console.log("已经到第一页了")
                }
            }}>上一页</Link>
            {pageEles}
            <Link to={`/list/${type}/${page < pageCount ? page + 1 : pageCount}`} onClick={() => {
                if (page === pageCount) {
                    console.log("已经到最后一页了")
                }
            }}>下一页</Link>
        </nav>
    )
}

export default Pagination